package com.pomr.controller.synthesize;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pomr.model.synthesize.Equipment;
import com.pomr.model.synthesize.Manufacturer;
import com.pomr.model.sys.Agency;
import com.pomr.model.sys.User;
import com.pomr.service.synthesize.ManufacturerService;
import com.pomr.service.sys.AgencyService;
import com.pomr.util.common.JWT;
import com.pomr.util.common.ResponseData;
import com.pomr.util.page.Page;
import com.pomr.util.page.PageParam;

@Controller
@RequestMapping("/manufacturer")
public class ManufacturerController {
	@Autowired
	@Qualifier("manufacturerServiceImpl")
	private ManufacturerService manufacturerService;
	@Autowired
	@Qualifier("agencyServiceImpl")
	private AgencyService agencyService;
	
	/**
	 * 添加厂商 名称不能为空
	 * @param manufacturer
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inter-add",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData addManufacturer(@RequestBody Manufacturer manufacturer,HttpServletRequest request){
		ResponseData pd = null;
		try{
			User user = JWT.findUserByToken(request);
			manufacturer.setUser(user);
			if(manufacturerService.saveManufacturer(manufacturer)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.fail();
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	/**
	 * 查询厂商信息  （名称、邮箱、联系方式、状态）
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inter-search",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<Manufacturer>> searchManufacturers(PageParam param,HttpServletRequest request){
		try{
			
			String page = param.getPage();
			String limit = param.getLimit();
			Map<String, String> params = param.getParams();
			String bh = params.get("bh");
			String mname = params.get("name");
			String memail = params.get("email");
			String mtel = params.get("tel");
			String mkjm = params.get("kjm");
			User user = JWT.findUserByToken(request);
			Page<List<Manufacturer>> pp = manufacturerService.searchManufacturersByCondition(mname, memail, mtel, mkjm,page, limit,bh);
			return pp;
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	/**
	 * 通过id 批量删除厂商信息
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inter-delete",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteManufacturers(@RequestBody Map<String, String> map,HttpServletRequest request){
		ResponseData pd = null;
		try {
			String ids = map.get("id");
			System.out.println(ids+"------");
			if(ids == null || ids.trim().equals("")){
				pd = ResponseData.custom("未上传删除信息");
			}else{
				if(manufacturerService.deleteManufacturersByIds(ids.split(","))){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.fail();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}

	/**
	 * 通过id获取厂商信息
	 * @return
	 */
	@RequestMapping(value="/inter-searchone",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData searchManufacturer(@RequestBody Map<String, String> map,HttpServletRequest request){
		ResponseData pd = null;
		try{
			String id = map.get("id");
			if(id == null || id.trim().equals("")){
				pd = ResponseData.custom("未获取到设备唯一编号");
			}else{
				Manufacturer e = manufacturerService.searchManufacturerById(id);
				if(e == null){
					pd = ResponseData.custom("未获取到对应的设备信息");
				}else{
					pd = ResponseData.ok();
					pd.putDataValue("manufacturer", e);
				}
				User user  = JWT.findUserByToken(request);
				List<Agency> agencys = agencyService.searchAgencysOfUser(user);
				if(agencys == null){
					pd = ResponseData.notFound();
				}else{
					pd.putDataValue("agencys", agencys);
				}
			}
			
		}catch (Exception e) {
			// TODO: handle exception
			pd = ResponseData.exceptionError();
		}
		return pd;
	}

	@RequestMapping(value="/inter-edit",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData editManufacturer(@RequestBody Manufacturer manufacturer,HttpServletRequest request){
		ResponseData pd = null;
		try {
			if(manufacturer==null || manufacturer.getId()==null){
				pd = ResponseData.custom("未获取到厂商唯一编号");
			}else if(manufacturer.getName()==null || manufacturer.getName().equals("")){
				pd = ResponseData.custom("厂商名称不能为空");
			}else{
				User user = JWT.findUserByToken(request);
				manufacturer.setUser(user);
				if(manufacturerService.editManufacturerById(manufacturer)){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.fail();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-editStatus",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData editManufacturerStatus(@RequestBody Manufacturer manufacturer,HttpServletRequest request){
		ResponseData pd = null;
		try {
			if(manufacturer==null || manufacturer.getId()==null){
				pd = ResponseData.custom("未获取到厂商唯一编号");
			}else{
				if(manufacturerService.editManufacturerStatusById(manufacturer)){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.fail();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
}
